package com.bruan.d1_io;

import java.io.*;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //案例复制照片
            inputStream = new FileInputStream("D:\\Desktop\\hhh.jpg");
            outputStream = new FileOutputStream("D:\\Desktop\\new_dir_test_idea\\hhh.jpg");
            byte[] buffer = new byte[1024];
            //从字节输入流写出到字节输出流
            int len;
            while ((len=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (outputStream != null && inputStream != null){
                //先创建后关掉
                inputStream.close();
                outputStream.close();
            }
        }
        copyFile("D:\\Desktop\\hhh.jpg","D:\\Desktop\\new_dir_test_idea\\hhh.jpg");
    }
    public static void copyFile(String file1,String file2){
        //jdk7新加入的释放资源方法
        try(
                InputStream inputStream = new FileInputStream(file1);
                OutputStream outputStream = new FileOutputStream(file2);
                MyConnection myConnection = new MyConnection();
                ){
            byte[] buffer = new byte[1024];
            //从字节输入流写出到字节输出流
            int len;
            System.out.println(myConnection);
            while ((len=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,len);
            }
            System.out.println("复制成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
